package com.engine.salary.report.web;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.report.entity.param.*;
import com.engine.salary.report.wrapper.SalaryStatisticsReportWrapper;
import com.engine.salary.util.ResponseResult;
import io.swagger.v3.oas.annotations.parameters.RequestBody;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import weaver.hrm.HrmUserVarify;
import weaver.hrm.User;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.StreamingOutput;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 薪酬统计报表
 * <p>Copyright: Copyright (c) 2022</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
@Slf4j
public class SalaryStatisticsReportController {

    private SalaryStatisticsReportWrapper getSalaryStatisticsReportWrapper(User user) {
        return ServiceUtil.getService(SalaryStatisticsReportWrapper.class, user);
    }


    /**
     * 薪酬统计报表列表
     *
     * @return
     */
    @POST
    @Path("/list")
    @Produces(MediaType.APPLICATION_JSON)
    public String list(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsReportQueryParam queryParam) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsReportQueryParam, List<Map<String, Object>>>(user).run(getSalaryStatisticsReportWrapper(user)::list, queryParam);
    }

    /**
     * 获取薪酬统计报表表单
     *
     * @return
     */
    @GET
    @Path("/getForm")
    @Produces(MediaType.APPLICATION_JSON)
    public String getForm(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, Map<String, Object>>(user).run(getSalaryStatisticsReportWrapper(user)::getFrom, id);
    }

    /**
     * 保存薪酬统计报表
     *
     * @param param
     * @return
     */
    @POST
    @Path("/save")
    @Produces(MediaType.APPLICATION_JSON)
    public String save(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsReportSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsReportSaveParam, String>(user).run(getSalaryStatisticsReportWrapper(user)::save, param);
    }

    /**
     * 删除薪酬统计报表
     *
     * @param ids
     * @return
     */
    @POST
    @Path("/delete")
    @Produces(MediaType.APPLICATION_JSON)
    public String delete(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody Collection<Long> ids) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Collection<Long>, Map<String, Object>>(user).run(getSalaryStatisticsReportWrapper(user)::delete, ids);
    }

    /**
     * 复制薪酬统计报表
     *
     * @param id
     * @return
     */
    @GET
    @Path("/duplicate")
    @Produces(MediaType.APPLICATION_JSON)
    public String duplicate(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, String>(user).run(getSalaryStatisticsReportWrapper(user)::duplicate, id);
    }

    /**
     * 获取薪酬统计报表查询条件
     *
     * @param request
     * @param response
     * @param id
     * @return
     */
    @GET
    @Path("/getSearchCondition")
    @Produces(MediaType.APPLICATION_JSON)
    public String getSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response, @QueryParam(value = "id") Long id) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<Long, Map<String, Object>>(user).run(getSalaryStatisticsReportWrapper(user)::getSearchCondition, id);
    }

    /**
     * 保存薪酬统计报表查询条件
     *
     * @param param
     * @return
     */
    @POST
    @Path("/saveSearchCondition")
    @Produces(MediaType.APPLICATION_JSON)
    public String saveSearchCondition(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsSearchConditionSaveParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsSearchConditionSaveParam, String>(user).run(getSalaryStatisticsReportWrapper(user)::saveSearchCondition, param);
    }

    /**
     * 获取薪酬统计报表数据
     *
     * @param param
     * @return
     */
    @POST
    @Path("/getData")
    @Produces(MediaType.APPLICATION_JSON)
    public String getData(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsReportDataQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsReportDataQueryParam, Map<String, Object>>(user).run(getSalaryStatisticsReportWrapper(user)::getData, param);
    }

    /**
     * 导出报表数据
     *
     * @return
     */
    @POST
    @Path("/exportData")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportData(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsReportDataQueryParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = getSalaryStatisticsReportWrapper(user).exportData(param);
            XSSFWorkbook workbook = (XSSFWorkbook) map.get("workbook");
            String time = LocalDate.now().toString();
            String fileName = map.get("fileName") + "-" + time;
            try {
                fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            StreamingOutput output = outputStream -> {
                workbook.write(outputStream);
                outputStream.flush();
            };
            response.setContentType("application/octet-stream");
            return Response.ok(output).header("Content-disposition", "attachment;filename=" + fileName).header("Cache-Control", "no-cache").build();
        } catch (Exception e) {
            log.error("核算人员范围导出异常", e);
            throw e;
        }
    }

    /**
     * 获取报表的透视数据信息
     *
     * @return
     */
    @POST
    @Path("/getDataPerspective")
    @Produces(MediaType.APPLICATION_JSON)
    public String getDataPerspective(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsDataPerspectiveQueryParam param) {
        User user = HrmUserVarify.getUser(request, response);
        return new ResponseResult<SalaryStatisticsDataPerspectiveQueryParam, Map<String, Object>>(user).run(getSalaryStatisticsReportWrapper(user)::getDataPerspective, param);
    }

    @POST
    @Path("/exportDataPerspective")
    @Produces(MediaType.APPLICATION_OCTET_STREAM)
    public Response exportDataPerspective(@Context HttpServletRequest request, @Context HttpServletResponse response, @RequestBody SalaryStatisticsDataPerspectiveQueryParam param) {
        try {
            User user = HrmUserVarify.getUser(request, response);
            Map<String, Object> map = getSalaryStatisticsReportWrapper(user).exportDataPerspective(param);
            XSSFWorkbook workbook = (XSSFWorkbook) map.get("workbook");
            String time = LocalDate.now().toString();
            String fileName = map.get("fileName") + "-" + time;
            try {
                fileName = URLEncoder.encode(fileName + ".xlsx", "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            StreamingOutput output = outputStream -> {
                workbook.write(outputStream);
                outputStream.flush();
            };
            response.setContentType("application/octet-stream");
            return Response.ok(output).header("Content-disposition", "attachment;filename=" + fileName).header("Cache-Control", "no-cache").build();
        } catch (Exception e) {
            log.error("报表明细内容导出异常", e);
            throw e;
        }
    }
}
